package is.ru.honn.rumblr.util;

import org.dom4j.Element;
import org.dom4j.Node;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class XmlUtil
{
  public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-mm-dd hh:MM:ss");

  public static String getXPathValue(Element document, String xpath)
  {
    Node node = document.selectSingleNode(xpath);
    return (node != null) ? node.getText() : null;
  }

  public static Date getXPathValueAsDate(Element element, String xpath) throws ParseException
  {
		String value = XmlUtil.getXPathValue(element, xpath);
    Date date = DATE_FORMAT.parse(value);
    return (value != null) ? DATE_FORMAT.parse(value) : null;
	}

  public static List<String> getMultipleValues(Element e, String tag)
  {
    List list = e.selectNodes(tag);
    List<String> tagList = new ArrayList<String>();
    Element eTag;
    for(Object o : list)
    {
      tagList.add(((Element) o).getText());
    }
    return tagList;
  }

}

